package com.TropicalFlavor.controller;

import com.TropicalFlavor.po.MarketUser;
import com.TropicalFlavor.service.userService;
import com.TropicalFlavor.tool.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class RegisterController
{

    @Resource
    private userService userService;

    @RequestMapping("/goToRegister.do")
    public String goToRegister(HttpServletRequest request)
    {
        return "/page/register1";
    }

    @RequestMapping("/registerUser.do")
    public String insertUser(HttpServletRequest request, @RequestParam String sNum, @RequestParam String Uname,
                                   @RequestParam String Password, @RequestParam String Email, @RequestParam String PhoneNum,
                                   @RequestParam String rePassword, @RequestParam String token)
    {
        String insertUserToken = (String)request.getSession().getAttribute("token");
        //String realUID = (String)request.getSession().getAttribute("UID");

        if(StringUtils.getInstance().isNullOrEmpty(insertUserToken) || !insertUserToken.equals(token))
        {
            return "redirect:/goToRegister.do";
        }
        if(sNum.isEmpty() || Uname.isEmpty() || Email.isEmpty()
                || PhoneNum.isEmpty() || Password.isEmpty() || rePassword.isEmpty())
            return "redirect:/goToRegister.do";

        MarketUser user = new MarketUser(Uname, Email, PhoneNum, Password, sNum, true);
        boolean isRegisterSuccess = userService.Register(user);
        if(isRegisterSuccess)
        {
            request.getSession().setAttribute("marketUser", user);
            return "redirect:/";
        }
        else
        {
            return "redirect:/goToRegister.do";
        }
    }

}
